package com.controller.purchase;

import java.io.IOException;
import java.net.URLEncoder;
import java.rmi.RemoteException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.google.protobuf.ServiceException;
import com.model.purchase.PurchaseInquiryResult;
import com.model.purchase.PurchaseInquiryResultVO;
import com.service.purchase.ResultService;

/**
 * 询价结果
 */
@Controller
@RequestMapping(value = "/api")
public class ResultController {

	static final Logger logger = LoggerFactory.getLogger(ResultController.class);
	@Autowired
	ResultService resultService;

	/**
	 * 查询列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/purchase/result/selectResultList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectResultList(@RequestParam Map<String, Object> map) {
		return resultService.selectResultList(map);
	}

	/**
	 * 查询详情
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/purchase/result/selectResultInfo", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectResultInfo(@RequestParam String resId) {
		return resultService.selectResultInfo(resId);
	}

	@RequestMapping(value = "/purchase/result/transfer", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult transfer(@RequestBody Map<String, Object> map) {
		return resultService.transfer(map);
	}

	/**
	 * 成交明细详情
	 * 
	 * @param resId
	 * @param infId
	 * @return
	 */
	@RequestMapping(value = "/purchase/result/selectResultMmInfo", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectResultMmInfo(@RequestParam String resId, @RequestParam String infId) {
		return resultService.selectResultMmInfo(resId, infId);
	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/purchase/result/exportMmList")
	public void exportMmList(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("询价书-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		resultService.exportMmList(map, response);

	}

	/**
	 * 保存询价结果
	 * 
	 * @param map
	 * @return
	 * @throws ServiceException
	 * @throws RemoteException
	 */
	@RequestMapping(value = "/purchase/result/saveResult", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult saveResult(@RequestBody PurchaseInquiryResultVO purchaseInquiryResultVO) {
		return resultService.saveResult(purchaseInquiryResultVO);
	}

	/**
	 * 提交询价结果
	 * 
	 * @param map
	 * @return
	 * @throws ServiceException
	 * @throws RemoteException
	 */
	@RequestMapping(value = "/purchase/result/commitResult", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult commitResult(@RequestBody PurchaseInquiryResultVO purchaseInquiryResultVO) {
		return resultService.commitResult(purchaseInquiryResultVO);
	}

	/**
	 * 撤回询价结果
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/purchase/result/back", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult back(@RequestBody PurchaseInquiryResult purchaseInquiryResultVO) {
		return resultService.back(purchaseInquiryResultVO);
	}

	@RequestMapping(value = "/purchase/result/backAdmin", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult backAdmin(@RequestBody PurchaseInquiryResult purchaseInquiryResultVO) {
		return resultService.backAdmin(purchaseInquiryResultVO);
	}

	@RequestMapping(value = "/purchase/result/commitAdmin", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult commitAdmin(@RequestBody PurchaseInquiryResultVO purchaseInquiryResultVO) {
		return resultService.commitAdmin(purchaseInquiryResultVO);
	}

	/**
	 * 删除询价
	 * 
	 * @param inqId
	 * @return
	 */
	@RequestMapping(value = "/purchase/result/delResult", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delResult(@RequestParam String resId) {
		return resultService.delResult(resId);
	}

	/**
	 * 删除询价
	 * 
	 * @param inqId
	 * @return
	 */
	@RequestMapping(value = "/purchase/result/delResultMm", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delResultMm(@RequestParam String resId, @RequestParam String infId, @RequestParam String planId) {
		return resultService.delResultMm(resId, infId, planId);
	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "/purchase/result/export")
	public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");

		String fileName = URLEncoder.encode("询价书-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		resultService.exportList(map, response);

	}
}
